import { ref } from 'vue'

export function useCreateCompany(baseUrl: string) {
  const loading = ref(false)
  const error = ref<string | null>(null)
  const result = ref<{ id: number } | null>(null)

  async function createCompany(payload: { name: string; comment: string }) {
    loading.value = true
    error.value = null
    try {
      const res = await fetch(`${baseUrl}/api/company/create`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload)
      })
      if (!res.ok) throw new Error('网络错误')
      result.value = await res.json()
    } catch (e: any) {
      error.value = e.message || '未知错误'
    } finally {
      loading.value = false
    }
  }

  return { createCompany, loading, error, result }
}
